﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Diagnostics;

namespace Celeriq.GeneratedTester
{
	class Program
	{
		static void Main(string[] args)
		{
			AddData();
			QueryData();

			Console.WriteLine();
			Console.WriteLine("Press any key...");
			Console.ReadLine();
		}

		private static void QueryData()
		{
			using (var factory = RepositoryConnection.GetFactory())
			{
				var service = factory.CreateChannel();
				var query = new ListingQuery();
				var masterResults = RepositoryConnection.QueryData(query, service);

				foreach (var d in masterResults.DimensionList)
				{
					foreach (var r in d.RefinementList)
					{
						query = new ListingQuery();
						//query.DimensionValueList.Add(r.DVIdx);
						query.FieldFilters.Add(new ListingQueryFilterAutoID() { AutoID = 4, Comparer = Common.ComparisonConstants.Equals });
						var results = RepositoryConnection.QueryData(query, service);
						Console.WriteLine("Count: " + results.TotalRecordCount + " / Time: " + results.ComputeTime + " / " + d.Name + " : " + r.FieldValue);
					}
				}
			}
		}

		private static void AddData()
		{
			var rnd = new Random();
			var makeList = new List<string>();
			var stateList = new List<string>();

			#region Test Data
			makeList.Add("Toyota");
			makeList.Add("Honda");
			makeList.Add("Audi");
			makeList.Add("VW");

			stateList.Add("Alabama");
			stateList.Add("Alaska");
			stateList.Add("Arizona");
			stateList.Add("Arkansas");
			stateList.Add("California");
			stateList.Add("Colorado");
			stateList.Add("Connecticut");
			stateList.Add("Delaware");
			stateList.Add("Florida");
			stateList.Add("Georgia");
			stateList.Add("Hawaii");
			stateList.Add("Idaho");
			stateList.Add("Illinois");
			stateList.Add("Indiana");

			#endregion

			#region Add Data
			int added = 0;
			const int MAX_ELEMENTS = 10000;
			using (var factory = RepositoryConnection.GetFactory())
			{
				var service = factory.CreateChannel();
				var query = new ListingQuery();
				var masterResults = RepositoryConnection.QueryData(query, service); //Hit first time to prime the pump
				var loopStart = masterResults.TotalRecordCount + 1;

				var timer = new Stopwatch();
				var list = new List<ListingItem>();
				timer.Start();
				for (int ii = loopStart; ii <= MAX_ELEMENTS; ii++)
				{
					var newItem = new ListingItem()
					{
						AutoID = ii,
						Location = new Common.GeoCode() { Latitude = GetLatitude(), Longitude = GetLongitude() },
						PostDate = new DateTime(rnd.Next(2000, 2010), rnd.Next(1, 13), rnd.Next(1, 29)),
						Price=rnd.Next(8000, 98000),
						State=stateList[rnd.Next(0, stateList.Count)],
						Year = rnd.Next(2000, 2010),
					
					};

					list.Add(newItem);

					if (list.Count % 200 == 0)
					{
						added += list.Count;
						RepositoryConnection.UpdateData(list, service);
						list = new List<ListingItem>();
						var itemsLeft = (MAX_ELEMENTS - ii);
						var msPerItem = ((timer.ElapsedMilliseconds * 1.0) / (ii - loopStart + 1));
						Console.WriteLine("Add count: " + ii.ToString("###,###,##0") + " | Elapsed/item: " + msPerItem.ToString("###,###,###0.00") + " | Items/s: " + (1000 / msPerItem).ToString("###,###,##0") + " | Time left: " + (itemsLeft * (msPerItem / 1000.0)).ToString("###,###,##0"));
					}
				}

				added += list.Count;
				if (list.Count > 0)
					RepositoryConnection.UpdateData(list, service);

				timer.Stop();

				if (added > 0)
					Console.WriteLine("Elapsed/item: " + (timer.ElapsedMilliseconds / (MAX_ELEMENTS - loopStart)).ToString("###,###,###0.0"));
			}
			#endregion
		}

		private static Random rnd = new Random();
		private const double PRECISION = 100000.0;
		public static double GetLatitude()
		{
			return rnd.Next(31, 46) + rnd.Next(0, (int)PRECISION) / PRECISION;
		}

		public static double GetLongitude()
		{
			return rnd.Next(-120, -82) + rnd.Next(0, (int)PRECISION) / PRECISION;
		}

	}
}